package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Account;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AccountDao {
    @Results(id = "accountResultMap", value = {
            @Result(column = "account_id", property = "accountId", typeHandler = IdTypeHandler.class),
            @Result(column = "balance", property = "balance"),
            @Result(column = "status", property = "status"),
    })
    @Select("select * from t_account where account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Account selectAccountByAccountId(String accountId);

    @ResultMap("accountResultMap")
    @Select("<script>" +
            "select * from t_account " +
            "where account_id in " +
            "<foreach item='id' collection='accountIdList' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    List<Account> selectAccountListByAccountIdList(List<String> accountIdList);

    @Insert("insert into t_account values " +
            "(#{accountId}, #{balance}, #{status})")
    Integer insertAccount(Account account);
    @Update("update t_account set " +
            "balance = balance + #{amount} " +
            "where account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer addBalanceByAccountId(String accountId, Double amount);

    @Update("update t_account set " +
            "status = #{status} " +
            "where account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setStatusByAccountId(String accountId, Integer status);
}
